<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>L2LB: heartbeat.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Gerado por Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;principal</span></a></li>
      <li><a href="annotated.html"><span>Estruturas&nbsp;de&nbsp;dados</span></a></li>
      <li class="current"><a href="files.html"><span>Ficheiros</span></a></li>
    </ul>
  </div>
<h1>heartbeat.h</h1><a href="heartbeat_8h.html">Ir para a documentação deste ficheiro.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00008"></a>00008 <span class="preprocessor">#ifndef HEARTBEAT_H_</span>
<a name="l00009"></a>00009 <span class="preprocessor"></span><span class="preprocessor">#define HEARTBEAT_H_</span>
<a name="l00010"></a>00010 <span class="preprocessor"></span>
<a name="l00015"></a><a class="code" href="heartbeat_8h.html#bd1e24640abcadec19ebb45ce2f73c10">00015</a> <span class="preprocessor">#define HEARTBEAT_TIMEOUT 10 * HZ</span>
<a name="l00016"></a>00016 <span class="preprocessor"></span>
<a name="l00020"></a><a class="code" href="heartbeat_8h.html#b573f5a033c8c8920ee38447f408bbca">00020</a> <span class="preprocessor">#define UDP_HEARTBEAT_PORT 1500 //2325 </span>
<a name="l00021"></a>00021 <span class="preprocessor"></span>
<a name="l00025"></a><a class="code" href="heartbeat_8h.html#b5e2712d83aef730daa0f547e6218e9f">00025</a> <span class="keywordtype">char</span> *<a class="code" href="heartbeat_8h.html#b5e2712d83aef730daa0f547e6218e9f" title="Mensagem enviada no Heartbeat.">hb_msg_buff</a>;
<a name="l00026"></a><a class="code" href="heartbeat_8h.html#adfc50704f2f70e43acd9d50b70f9a24">00026</a> <span class="keyword">volatile</span> <span class="keywordtype">int</span> <a class="code" href="heartbeat_8h.html#adfc50704f2f70e43acd9d50b70f9a24">hb_msg_buff_len</a> = 0;
<a name="l00027"></a>00027 
<a name="l00031"></a><a class="code" href="heartbeat_8h.html#63e6914e51b6e79ae83f8c548c7a9e70">00031</a> <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="heartbeat_8h.html#63e6914e51b6e79ae83f8c548c7a9e70" title="Envia a mensagem em Broadcast?">broadcast</a> = 1;
<a name="l00032"></a>00032 
<a name="l00038"></a><a class="code" href="heartbeat_8h.html#1cacc18d29de69dfe9b47ee4eb5e7600">00038</a> <span class="keywordtype">void</span> <a class="code" href="cluster_8h.html#1cacc18d29de69dfe9b47ee4eb5e7600" title="Prepara o servidor adicionado ao cluster para ser removido do cluster se nao receber...">hb_timer_add</a>(<span class="keyword">struct</span> <a class="code" href="structcluster__node.html" title="Nó do cluster.">cluster_node</a> *new_node)
<a name="l00039"></a>00039 {
<a name="l00040"></a>00040         <span class="keyword">struct </span>timer_list *hb_timer;
<a name="l00041"></a>00041         
<a name="l00042"></a>00042         hb_timer = (<span class="keyword">struct </span>timer_list *)kmalloc(<span class="keyword">sizeof</span> (<span class="keyword">struct</span> timer_list), GFP_ATOMIC);
<a name="l00043"></a>00043         
<a name="l00044"></a>00044         init_timer(hb_timer);
<a name="l00045"></a>00045         hb_timer-&gt;function = &amp;<a class="code" href="cluster_8h.html#c55a15a79cadec11fb3d6df11ac5f4e7" title="Remove um servidor do clusterRemove o servidor da lista de servidores,Notifica os...">remove_cluster_node</a>;
<a name="l00046"></a>00046         hb_timer-&gt;data = (<span class="keywordtype">unsigned</span> long)new_node;
<a name="l00047"></a>00047         hb_timer-&gt;expires = jiffies + <a class="code" href="heartbeat_8h.html#bd1e24640abcadec19ebb45ce2f73c10" title="Quantos segundos no maximo o cluster deve aguardar antes de remover um servidor do...">HEARTBEAT_TIMEOUT</a>;
<a name="l00048"></a>00048         
<a name="l00049"></a>00049         new_node-&gt;<a class="code" href="structcluster__node.html#69957993737a06530c6d09de347c0348">hb_timer</a> = hb_timer; 
<a name="l00050"></a>00050         add_timer(hb_timer);
<a name="l00051"></a>00051 }
<a name="l00052"></a>00052 
<a name="l00058"></a><a class="code" href="heartbeat_8h.html#a05383ca38bdc4afb4d20da4bfe14245">00058</a> <span class="keywordtype">void</span> <a class="code" href="heartbeat_8h.html#a05383ca38bdc4afb4d20da4bfe14245" title="Renova o timer do Heartbeat, caso seja recebido o heartbeat do servidor.">hb_timer_renew</a>(<span class="keyword">struct</span> <a class="code" href="structcluster__node.html" title="Nó do cluster.">cluster_node</a> *node)
<a name="l00059"></a>00059 {
<a name="l00060"></a>00060         mod_timer(node-&gt;<a class="code" href="structcluster__node.html#69957993737a06530c6d09de347c0348">hb_timer</a>, jiffies + <a class="code" href="heartbeat_8h.html#bd1e24640abcadec19ebb45ce2f73c10" title="Quantos segundos no maximo o cluster deve aguardar antes de remover um servidor do...">HEARTBEAT_TIMEOUT</a> );
<a name="l00061"></a>00061 }
<a name="l00062"></a>00062 
<a name="l00066"></a><a class="code" href="heartbeat_8h.html#9a09c37d118a488eb9427f761be51f08">00066</a> <span class="keyword">struct </span><a class="code" href="structksock__thread__t.html" title="Thread usada para estabelecer conexões socket.">ksock_thread_t</a> *<a class="code" href="heartbeat_8h.html#9a09c37d118a488eb9427f761be51f08" title="Thread para o socket do servidor UDP.">hb_udp_server_thread</a> = NULL;
<a name="l00067"></a>00067 
<a name="l00071"></a><a class="code" href="heartbeat_8h.html#47b8a08bfc4c35b86ff52bce2783d51a">00071</a> <span class="keyword">struct </span><a class="code" href="structksock__thread__t.html" title="Thread usada para estabelecer conexões socket.">ksock_thread_t</a> *<a class="code" href="heartbeat_8h.html#47b8a08bfc4c35b86ff52bce2783d51a" title="Thread para o sockete do cliente UDP.">hb_udp_client_thread</a> = NULL;
<a name="l00072"></a>00072 
<a name="l00079"></a><a class="code" href="heartbeat_8h.html#25dbc05b5c8157863bcb1801c17f77c1">00079</a> <span class="keywordtype">int</span> <a class="code" href="cluster_8h.html#25dbc05b5c8157863bcb1801c17f77c1" title="Envia uma mensagem em Brodcast para todos os nós do cluster.">hb_broadcast_msg</a>(<span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> buf_len)
<a name="l00080"></a>00080 {
<a name="l00081"></a>00081         <span class="keywordtype">int</span> size;
<a name="l00082"></a>00082         
<a name="l00083"></a>00083         size = <a class="code" href="socket_8h.html#c3204901b688e3cb793a9b01c5d19c9a" title="Envia mensagem no socket.">ksocket_send</a>(hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>, &amp;hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#1bada36c1d4378aa03ec5eb35b6e8d6b">addr_send</a>, buf, buf_len);
<a name="l00084"></a>00084         
<a name="l00085"></a>00085         <span class="keywordflow">if</span> (size &lt; 0)
<a name="l00086"></a>00086                 printk(KERN_ALERT MODULE_NAME<span class="stringliteral">": error setting datagram, sock_sendmsg error = %d\n"</span>, size);
<a name="l00087"></a>00087         
<a name="l00088"></a>00088         <span class="keywordflow">return</span> size;
<a name="l00089"></a>00089 }
<a name="l00090"></a>00090 
<a name="l00094"></a><a class="code" href="heartbeat_8h.html#8c2bcf74f2baf343f7ee6f8cf8d26aaf">00094</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="heartbeat_8h.html#8c2bcf74f2baf343f7ee6f8cf8d26aaf" title="Re-cria a mensagem de envio do heartbeat.">hb_rebuild_msg</a>(<span class="keywordtype">void</span>)
<a name="l00095"></a>00095 {
<a name="l00096"></a>00096         <span class="keywordflow">if</span>(!<a class="code" href="cluster_8h.html#1917b2709741dea7ce5cf6ea441686ed" title="Cluster.">my_cluster</a>.<a class="code" href="structcluster.html#7ccd7197eb601ffee1b0f6209578e9a2">my_node</a>) 
<a name="l00097"></a>00097         {
<a name="l00098"></a>00098                 <span class="keywordflow">if</span>(<a class="code" href="heartbeat_8h.html#adfc50704f2f70e43acd9d50b70f9a24">hb_msg_buff_len</a> &gt; 0) 
<a name="l00099"></a>00099                 {
<a name="l00100"></a>00100                         kfree(<a class="code" href="heartbeat_8h.html#b5e2712d83aef730daa0f547e6218e9f" title="Mensagem enviada no Heartbeat.">hb_msg_buff</a>);
<a name="l00101"></a>00101                         <a class="code" href="heartbeat_8h.html#adfc50704f2f70e43acd9d50b70f9a24">hb_msg_buff_len</a> = 0;
<a name="l00102"></a>00102                 }
<a name="l00103"></a>00103                 <span class="keywordflow">return</span>;
<a name="l00104"></a>00104         }
<a name="l00105"></a>00105         
<a name="l00106"></a>00106         <span class="keywordflow">if</span>(<a class="code" href="heartbeat_8h.html#adfc50704f2f70e43acd9d50b70f9a24">hb_msg_buff_len</a> == 0) 
<a name="l00107"></a>00107         {
<a name="l00108"></a>00108                 <a class="code" href="heartbeat_8h.html#b5e2712d83aef730daa0f547e6218e9f" title="Mensagem enviada no Heartbeat.">hb_msg_buff</a> = (<span class="keywordtype">char</span> *)kmalloc(12, GFP_ATOMIC);
<a name="l00109"></a>00109                 memset(<a class="code" href="heartbeat_8h.html#b5e2712d83aef730daa0f547e6218e9f" title="Mensagem enviada no Heartbeat.">hb_msg_buff</a>, 0, 12);
<a name="l00110"></a>00110         }
<a name="l00111"></a>00111         
<a name="l00112"></a>00112         sprintf(<a class="code" href="heartbeat_8h.html#b5e2712d83aef730daa0f547e6218e9f" title="Mensagem enviada no Heartbeat.">hb_msg_buff</a>, <span class="stringliteral">"hb :%d,%d,%d"</span>, <a class="code" href="cluster_8h.html#1917b2709741dea7ce5cf6ea441686ed" title="Cluster.">my_cluster</a>.<a class="code" href="structcluster.html#7ccd7197eb601ffee1b0f6209578e9a2">my_node</a>-&gt;<a class="code" href="structcluster__node.html#7f6ec5e953944d0fce058aa0490cf8b3">id</a>, <a class="code" href="cluster_8h.html#1917b2709741dea7ce5cf6ea441686ed" title="Cluster.">my_cluster</a>.<a class="code" href="structcluster.html#c5a12bfd4d88406ced444aedaf84ed45">node_count</a>, <a class="code" href="cluster_8h.html#1917b2709741dea7ce5cf6ea441686ed" title="Cluster.">my_cluster</a>.<a class="code" href="structcluster.html#0790d37c29c68e44e2e6aa3c0d46bb70">next_serving</a>-&gt;<a class="code" href="structcluster__node.html#7f6ec5e953944d0fce058aa0490cf8b3">id</a>);
<a name="l00113"></a>00113         <a class="code" href="heartbeat_8h.html#adfc50704f2f70e43acd9d50b70f9a24">hb_msg_buff_len</a> = 12;
<a name="l00114"></a>00114 }
<a name="l00115"></a>00115 
<a name="l00121"></a><a class="code" href="heartbeat_8h.html#c6c61bdfb86af95a46983e4ac5ded3e5">00121</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="heartbeat_8h.html#c6c61bdfb86af95a46983e4ac5ded3e5" title="Carrega uma mensagem de heartbear.">nlb_load_heartbeat_signal</a>(<span class="keywordtype">char</span> *msg)
<a name="l00122"></a>00122 {
<a name="l00123"></a>00123         <span class="keyword">static</span> <span class="keywordtype">char</span> msg_id[1024];
<a name="l00124"></a>00124         <span class="keyword">static</span> <span class="keywordtype">char</span> msg_args[1024];
<a name="l00125"></a>00125         <span class="keyword">static</span> <span class="keywordtype">int</span> myNode, nodeCount, nextNode, myId;
<a name="l00126"></a>00126         <span class="keyword">struct </span><a class="code" href="structcluster__node.html" title="Nó do cluster.">cluster_node</a> *node;
<a name="l00127"></a>00127         
<a name="l00128"></a>00128         sscanf(msg, <span class="stringliteral">"%s :%s"</span>, (<span class="keywordtype">char</span> *)&amp;msg_id, (<span class="keywordtype">char</span> *)&amp;msg_args);
<a name="l00129"></a>00129         
<a name="l00130"></a>00130         <span class="keywordflow">if</span>(strcmp(msg_id,<span class="stringliteral">"hb"</span>) == 0)
<a name="l00131"></a>00131         {
<a name="l00132"></a>00132                 sscanf((<span class="keywordtype">char</span> *)&amp;msg_args, <span class="stringliteral">"%d,%d,%d"</span>, &amp;myNode, &amp;nodeCount, &amp;nextNode);
<a name="l00133"></a>00133                 
<a name="l00134"></a>00134                 node = <a class="code" href="cluster_8h.html#4e116ec9395a6720c5f84de3e28b25a9" title="Busca um servidor do cluster pelo ID.">get_node_by_id</a>(myNode);
<a name="l00135"></a>00135 
<a name="l00136"></a>00136                 <span class="keywordflow">if</span>(!node)
<a name="l00137"></a>00137                 {
<a name="l00138"></a>00138                         printk(KERN_ALERT MODULE_NAME <span class="stringliteral">": Heartbeat recebido de servidor NAO cadastrado (%d!)\n"</span>, myNode);
<a name="l00139"></a>00139                         <span class="keywordflow">return</span>;
<a name="l00140"></a>00140                 }
<a name="l00141"></a>00141 
<a name="l00142"></a>00142                 <a class="code" href="heartbeat_8h.html#a05383ca38bdc4afb4d20da4bfe14245" title="Renova o timer do Heartbeat, caso seja recebido o heartbeat do servidor.">hb_timer_renew</a>(node);           
<a name="l00143"></a>00143         }
<a name="l00144"></a>00144         <span class="keywordflow">else</span> <span class="keywordflow">if</span>(strcmp(msg_id,<span class="stringliteral">"remove-node"</span>) == 0)
<a name="l00145"></a>00145         {
<a name="l00146"></a>00146                 sscanf((<span class="keywordtype">char</span> *)&amp;msg_args, <span class="stringliteral">"%d"</span>, &amp;myId);
<a name="l00147"></a>00147                 
<a name="l00148"></a>00148                 node = <a class="code" href="cluster_8h.html#4e116ec9395a6720c5f84de3e28b25a9" title="Busca um servidor do cluster pelo ID.">get_node_by_id</a>(myId);
<a name="l00149"></a>00149 
<a name="l00150"></a>00150                 <span class="keywordflow">if</span>(!node)
<a name="l00151"></a>00151                 {
<a name="l00152"></a>00152                         printk(KERN_ALERT MODULE_NAME <span class="stringliteral">": Remove-node recebido de servidor NAO cadastrado (%d!)\n"</span>, myId);
<a name="l00153"></a>00153                         <span class="keywordflow">return</span>;
<a name="l00154"></a>00154                 }
<a name="l00155"></a>00155                 
<a name="l00156"></a>00156                 <a class="code" href="cluster_8h.html#c55a15a79cadec11fb3d6df11ac5f4e7" title="Remove um servidor do clusterRemove o servidor da lista de servidores,Notifica os...">remove_cluster_node</a>((<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)node);
<a name="l00157"></a>00157         } 
<a name="l00158"></a>00158         <span class="keywordflow">else</span> 
<a name="l00159"></a>00159         {
<a name="l00160"></a>00160                 printk(KERN_ALERT MODULE_NAME <span class="stringliteral">": Mensagem de heartbeat invalida! (%s)\n"</span>, msg);
<a name="l00161"></a>00161         }
<a name="l00162"></a>00162 }
<a name="l00163"></a>00163 
<a name="l00168"></a><a class="code" href="heartbeat_8h.html#d969b6fda1c74c864aaa7587b1d46be4">00168</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="heartbeat_8h.html#d969b6fda1c74c864aaa7587b1d46be4" title="Inicia o servidor UDP que irá RECEBER as mensagens de Heartbeat dos servidores do...">hb_udb_server_start</a>(<span class="keywordtype">void</span>)
<a name="l00169"></a>00169 {
<a name="l00170"></a>00170     <span class="keywordtype">int</span> size, err;
<a name="l00171"></a>00171     <span class="keywordtype">int</span> bufsize = 1024;
<a name="l00172"></a>00172     <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> buf[bufsize+1];
<a name="l00173"></a>00173 
<a name="l00174"></a>00174     <span class="comment">/* kernel thread initialization */</span>
<a name="l00175"></a>00175     lock_kernel();
<a name="l00176"></a>00176     hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#f2c4b6bbf29b36dd0bbf5cf97205330a" title="Sinaliza se a thread está em execução (flag).">running</a> = 1;
<a name="l00177"></a>00177     current-&gt;flags |= PF_NOFREEZE;
<a name="l00178"></a>00178 
<a name="l00179"></a>00179     <span class="comment">/* daemonize (take care with signals, after daemonize() they are disabled) */</span>
<a name="l00180"></a>00180     daemonize(MODULE_NAME);
<a name="l00181"></a>00181     allow_signal(SIGKILL);
<a name="l00182"></a>00182     unlock_kernel();
<a name="l00183"></a>00183 
<a name="l00184"></a>00184     <span class="comment">/* create a socket */</span>
<a name="l00185"></a>00185     <span class="keywordflow">if</span> ( ( err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &amp;hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a> ) ) &lt; 0 )
<a name="l00186"></a>00186     {
<a name="l00187"></a>00187                 printk(KERN_ALERT MODULE_NAME<span class="stringliteral">": Could not create a datagram socket, error = %d\n"</span>, -ENXIO);
<a name="l00188"></a>00188                 <span class="keywordflow">goto</span> out;
<a name="l00189"></a>00189     }
<a name="l00190"></a>00190     
<a name="l00191"></a>00191     <span class="comment">/* configura BROADCAST */</span>
<a name="l00192"></a>00192     <span class="keywordflow">if</span>( (hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>-&gt;ops-&gt;setsockopt(hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>, SOL_SOCKET, SO_BROADCAST, (<span class="keywordtype">char</span> *)&amp;<a class="code" href="heartbeat_8h.html#63e6914e51b6e79ae83f8c548c7a9e70" title="Envia a mensagem em Broadcast?">broadcast</a>, <span class="keyword">sizeof</span> broadcast)) == -1 )
<a name="l00193"></a>00193     {
<a name="l00194"></a>00194         printk(KERN_ALERT MODULE_NAME<span class="stringliteral">": Could not configure broadcast, error = %d\n"</span>, -ENXIO);
<a name="l00195"></a>00195         <span class="keywordflow">goto</span> out;
<a name="l00196"></a>00196         }
<a name="l00197"></a>00197 
<a name="l00198"></a>00198     memset(&amp;hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">addr</a>, 0, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> sockaddr));
<a name="l00199"></a>00199     hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">addr</a>.sin_family               = AF_INET;
<a name="l00200"></a>00200     hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">addr</a>.sin_addr.s_addr  = htonl(INADDR_ANY);
<a name="l00201"></a>00201     hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">addr</a>.sin_port                 = htons(<a class="code" href="heartbeat_8h.html#b573f5a033c8c8920ee38447f408bbca" title="Porta UDP usada no envio e recebimento dos Heartbeats.">UDP_HEARTBEAT_PORT</a>);
<a name="l00202"></a>00202 
<a name="l00203"></a>00203     <span class="keywordflow">if</span> ( ( (err = hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>-&gt;ops-&gt;bind(hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>, (<span class="keyword">struct</span> sockaddr *)&amp;hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">addr</a>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> sockaddr) ) ) &lt; 0) )
<a name="l00204"></a>00204     {
<a name="l00205"></a>00205             printk(KERN_ALERT MODULE_NAME<span class="stringliteral">": Could not bind to socket port, error = %d\n"</span>, -err);
<a name="l00206"></a>00206             <span class="keywordflow">goto</span> close_and_out;
<a name="l00207"></a>00207     }
<a name="l00208"></a>00208 
<a name="l00209"></a>00209     printk(KERN_ALERT MODULE_NAME<span class="stringliteral">": listening on port %d\n"</span>, <a class="code" href="heartbeat_8h.html#b573f5a033c8c8920ee38447f408bbca" title="Porta UDP usada no envio e recebimento dos Heartbeats.">UDP_HEARTBEAT_PORT</a>);
<a name="l00210"></a>00210 
<a name="l00211"></a>00211     <span class="comment">/* main loop */</span>
<a name="l00212"></a>00212     memset(&amp;buf, 0, bufsize+1);
<a name="l00213"></a>00213     
<a name="l00214"></a>00214     <span class="keywordflow">for</span> (;;)
<a name="l00215"></a>00215     {   
<a name="l00216"></a>00216         size = <a class="code" href="socket_8h.html#754ef7ec12b37fc69e125121011fcec8" title="Recebe mensagem no socket.">ksocket_receive</a>(hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>, &amp;hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">addr</a>, buf, bufsize);
<a name="l00217"></a>00217         
<a name="l00218"></a>00218         <span class="keywordflow">if</span> (signal_pending(current))
<a name="l00219"></a>00219                 <span class="keywordflow">break</span>;
<a name="l00220"></a>00220 
<a name="l00221"></a>00221         <span class="keywordflow">if</span> (size &lt; 0)
<a name="l00222"></a>00222                 printk(KERN_ALERT MODULE_NAME <span class="stringliteral">": error getting datagram, sock_recvmsg error = %d\n"</span>, size);
<a name="l00223"></a>00223         <span class="keywordflow">else</span> 
<a name="l00224"></a>00224                 <a class="code" href="heartbeat_8h.html#c6c61bdfb86af95a46983e4ac5ded3e5" title="Carrega uma mensagem de heartbear.">nlb_load_heartbeat_signal</a>(buf);
<a name="l00225"></a>00225     }
<a name="l00226"></a>00226 
<a name="l00227"></a>00227 close_and_out:
<a name="l00228"></a>00228     sock_release(hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>);
<a name="l00229"></a>00229     hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a> = NULL;
<a name="l00230"></a>00230 
<a name="l00231"></a>00231 out:
<a name="l00232"></a>00232     hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#34e131d5c7d4d36b7213114ebdf869c0" title="Thread onde o socket está executando.">thread</a> = NULL;
<a name="l00233"></a>00233     hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#f2c4b6bbf29b36dd0bbf5cf97205330a" title="Sinaliza se a thread está em execução (flag).">running</a> = 0;
<a name="l00234"></a>00234 }
<a name="l00235"></a>00235 
<a name="l00240"></a><a class="code" href="heartbeat_8h.html#795b268047cc6c36f223e0fdcc5d4583">00240</a> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="heartbeat_8h.html#795b268047cc6c36f223e0fdcc5d4583" title="Iniciar o cliente UDP que irá ENVIAR as mensagens de heartbeat deste servidor do...">hb_udb_client_start</a>(<span class="keywordtype">void</span>)
<a name="l00241"></a>00241 {
<a name="l00242"></a>00242     <span class="keywordtype">int</span> err;
<a name="l00243"></a>00243     <span class="keywordtype">int</span> bufsize = 10;
<a name="l00244"></a>00244     <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> buf[bufsize+1];
<a name="l00245"></a>00245 
<a name="l00246"></a>00246     <span class="comment">/* kernel thread initialization */</span>
<a name="l00247"></a>00247     lock_kernel();
<a name="l00248"></a>00248     hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#f2c4b6bbf29b36dd0bbf5cf97205330a" title="Sinaliza se a thread está em execução (flag).">running</a> = 1;
<a name="l00249"></a>00249     current-&gt;flags |= PF_NOFREEZE;
<a name="l00250"></a>00250 
<a name="l00251"></a>00251     <span class="comment">/* daemonize (take care with signals, after daemonize() they are disabled) */</span>
<a name="l00252"></a>00252     daemonize(MODULE_NAME);
<a name="l00253"></a>00253     allow_signal(SIGKILL);
<a name="l00254"></a>00254     unlock_kernel();
<a name="l00255"></a>00255 
<a name="l00256"></a>00256     <span class="comment">/* create a socket */</span>
<a name="l00257"></a>00257     <span class="keywordflow">if</span> ( ( err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &amp;hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a> ) ) &lt; 0 )
<a name="l00258"></a>00258     {
<a name="l00259"></a>00259             printk(KERN_INFO MODULE_NAME<span class="stringliteral">": Could not create a datagram socket, error = %d\n"</span>, -ENXIO);
<a name="l00260"></a>00260             <span class="keywordflow">goto</span> out;
<a name="l00261"></a>00261     }
<a name="l00262"></a>00262 
<a name="l00263"></a>00263     <span class="comment">/* configura BROADCAST */</span>
<a name="l00264"></a>00264     <span class="keywordflow">if</span>( (hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>-&gt;ops-&gt;setsockopt(hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>, SOL_SOCKET, SO_BROADCAST, (<span class="keywordtype">char</span> *)&amp;<a class="code" href="heartbeat_8h.html#63e6914e51b6e79ae83f8c548c7a9e70" title="Envia a mensagem em Broadcast?">broadcast</a>, <span class="keyword">sizeof</span> broadcast)) == -1 )
<a name="l00265"></a>00265     {
<a name="l00266"></a>00266         printk(KERN_ALERT MODULE_NAME<span class="stringliteral">": Could not configure broadcast, error = %d\n"</span>, -ENXIO);
<a name="l00267"></a>00267         <span class="keywordflow">goto</span> out;
<a name="l00268"></a>00268         }
<a name="l00269"></a>00269     
<a name="l00270"></a>00270     memset(&amp;hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">addr</a>, 0, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> sockaddr));
<a name="l00271"></a>00271     hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">addr</a>.sin_family               = AF_INET;
<a name="l00272"></a>00272     hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">addr</a>.sin_addr.s_addr  = htonl(INADDR_BROADCAST);
<a name="l00273"></a>00273     hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">addr</a>.sin_port                 = htons(0);
<a name="l00274"></a>00274 
<a name="l00275"></a>00275     hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#1bada36c1d4378aa03ec5eb35b6e8d6b">addr_send</a>.sin_family          = AF_INET;
<a name="l00276"></a>00276     hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#1bada36c1d4378aa03ec5eb35b6e8d6b">addr_send</a>.sin_port            = htons(<a class="code" href="heartbeat_8h.html#b573f5a033c8c8920ee38447f408bbca" title="Porta UDP usada no envio e recebimento dos Heartbeats.">UDP_HEARTBEAT_PORT</a>);
<a name="l00277"></a>00277     hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#1bada36c1d4378aa03ec5eb35b6e8d6b">addr_send</a>.sin_addr.s_addr     = htonl(<a class="code" href="socket_8h.html#a260f09b00594e59211ccbbac88213bf">INADDR_SEND</a>);
<a name="l00278"></a>00278     
<a name="l00279"></a>00279     printk( KERN_ALERT MODULE_NAME <span class="stringliteral">": %u\n"</span>, INADDR_BROADCAST );
<a name="l00280"></a>00280     
<a name="l00281"></a>00281     <span class="comment">//parse_ipv4_addr("255.255.255.255", &amp;hb_udp_client_thread-&gt;addr_send.sin_addr.s_addr);</span>
<a name="l00282"></a>00282     <span class="comment">//hb_udp_client_thread-&gt;addr_send.sin_addr.s_addr   = htonl(hb_udp_client_thread-&gt;addr_send.sin_addr.s_addr);</span>
<a name="l00283"></a>00283 
<a name="l00284"></a>00284     <span class="keywordflow">if</span> ( ( (err = hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>-&gt;ops-&gt;bind(hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>, (<span class="keyword">struct</span> sockaddr *)&amp;hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">addr</a>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> sockaddr) ) ) &lt; 0) )
<a name="l00285"></a>00285     {
<a name="l00286"></a>00286             printk(KERN_ALERT MODULE_NAME<span class="stringliteral">": Could not bind to socket port, error = %d\n"</span>, -err);
<a name="l00287"></a>00287             <span class="keywordflow">goto</span> close_and_out;
<a name="l00288"></a>00288     }
<a name="l00289"></a>00289 
<a name="l00290"></a>00290     sprintf(buf, <span class="stringliteral">"heartbeat"</span>);
<a name="l00291"></a>00291     
<a name="l00292"></a>00292     <span class="comment">/* main loop */</span>  
<a name="l00293"></a>00293     <span class="keywordflow">for</span> (;;)
<a name="l00294"></a>00294     {
<a name="l00295"></a>00295         ssleep(1);
<a name="l00296"></a>00296         
<a name="l00297"></a>00297         <span class="keywordflow">if</span> (signal_pending(current))
<a name="l00298"></a>00298                 <span class="keywordflow">break</span>;
<a name="l00299"></a>00299         
<a name="l00300"></a>00300         <span class="keywordflow">if</span>(<a class="code" href="heartbeat_8h.html#adfc50704f2f70e43acd9d50b70f9a24">hb_msg_buff_len</a> == 0)
<a name="l00301"></a>00301                 <span class="keywordflow">continue</span>;
<a name="l00302"></a>00302                 
<a name="l00303"></a>00303         <a class="code" href="cluster_8h.html#25dbc05b5c8157863bcb1801c17f77c1" title="Envia uma mensagem em Brodcast para todos os nós do cluster.">hb_broadcast_msg</a>(<a class="code" href="heartbeat_8h.html#b5e2712d83aef730daa0f547e6218e9f" title="Mensagem enviada no Heartbeat.">hb_msg_buff</a>, <a class="code" href="heartbeat_8h.html#adfc50704f2f70e43acd9d50b70f9a24">hb_msg_buff_len</a>);
<a name="l00304"></a>00304     }
<a name="l00305"></a>00305 
<a name="l00306"></a>00306 close_and_out:
<a name="l00307"></a>00307     sock_release(hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>);
<a name="l00308"></a>00308     hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a> = NULL;
<a name="l00309"></a>00309 
<a name="l00310"></a>00310 out:
<a name="l00311"></a>00311         hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#34e131d5c7d4d36b7213114ebdf869c0" title="Thread onde o socket está executando.">thread</a> = NULL;
<a name="l00312"></a>00312         hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#f2c4b6bbf29b36dd0bbf5cf97205330a" title="Sinaliza se a thread está em execução (flag).">running</a> = 0;
<a name="l00313"></a>00313 
<a name="l00314"></a>00314 }
<a name="l00315"></a>00315 
<a name="l00316"></a><a class="code" href="heartbeat_8h.html#95053b46f210b2f1451e3314bd4e4221">00316</a> <span class="keyword">static</span> <span class="keyword">struct </span><a class="code" href="structksock__thread__t.html" title="Thread usada para estabelecer conexões socket.">ksock_thread_t</a> **<a class="code" href="heartbeat_8h.html#95053b46f210b2f1451e3314bd4e4221">hb_get_udp_server_thread</a>(<span class="keywordtype">void</span>)
<a name="l00317"></a>00317 {
<a name="l00318"></a>00318         hb_udp_server_thread = kmalloc(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structksock__thread__t.html" title="Thread usada para estabelecer conexões socket.">ksock_thread_t</a>), GFP_KERNEL);
<a name="l00319"></a>00319         memset(hb_udp_server_thread, 0, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structksock__thread__t.html" title="Thread usada para estabelecer conexões socket.">ksock_thread_t</a>));
<a name="l00320"></a>00320         
<a name="l00321"></a>00321         hb_udp_server_thread-&gt;<a class="code" href="structksock__thread__t.html#2fd9f49a71f307cb4f2c1ca3940af8cb">fn</a> = &amp;<a class="code" href="heartbeat_8h.html#d969b6fda1c74c864aaa7587b1d46be4" title="Inicia o servidor UDP que irá RECEBER as mensagens de Heartbeat dos servidores do...">hb_udb_server_start</a>;
<a name="l00322"></a>00322         
<a name="l00323"></a>00323         <span class="keywordflow">return</span> &amp;hb_udp_server_thread;
<a name="l00324"></a>00324 }
<a name="l00325"></a>00325 
<a name="l00326"></a><a class="code" href="heartbeat_8h.html#61f03f1313c2bf412a60f54162b287da">00326</a> <span class="keyword">static</span> <span class="keyword">struct </span><a class="code" href="structksock__thread__t.html" title="Thread usada para estabelecer conexões socket.">ksock_thread_t</a> **<a class="code" href="heartbeat_8h.html#61f03f1313c2bf412a60f54162b287da">hb_get_udp_client_thread</a>(<span class="keywordtype">void</span>)
<a name="l00327"></a>00327 {
<a name="l00328"></a>00328         hb_udp_client_thread = kmalloc(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structksock__thread__t.html" title="Thread usada para estabelecer conexões socket.">ksock_thread_t</a>), GFP_KERNEL);
<a name="l00329"></a>00329         memset(hb_udp_client_thread, 0, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structksock__thread__t.html" title="Thread usada para estabelecer conexões socket.">ksock_thread_t</a>));
<a name="l00330"></a>00330         
<a name="l00331"></a>00331         hb_udp_client_thread-&gt;<a class="code" href="structksock__thread__t.html#2fd9f49a71f307cb4f2c1ca3940af8cb">fn</a> = &amp;<a class="code" href="heartbeat_8h.html#795b268047cc6c36f223e0fdcc5d4583" title="Iniciar o cliente UDP que irá ENVIAR as mensagens de heartbeat deste servidor do...">hb_udb_client_start</a>;
<a name="l00332"></a>00332         
<a name="l00333"></a>00333         <span class="keywordflow">return</span> &amp;hb_udp_client_thread;
<a name="l00334"></a>00334 }
<a name="l00335"></a>00335 
<a name="l00336"></a>00336 <span class="preprocessor">#endif </span><span class="comment">/*HEARTBEAT_H_*/</span>
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Gerado em Wed Sep 10 23:05:55 2008 para L2LB por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>
